package com.earstep.threadpool;

import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TimerTasks extends Thread {
	private static Log logger = LogFactory.getLog(TimerTasks.class);
	// 任务列表
	private Vector<TimerTask> tasks = new Vector<TimerTask>();
	// 当前是否处于空闲状态
	private boolean suspended = false;
	// 当前是否处于睡眠状态
	private boolean sleeping = false;

	@Override
	public void run() {
		// 线程无限循环
		while (true) {
			long sleepTime = 0;
			synchronized (tasks) {
				sleepTime = scan();
				purge();
			}

			// 没有任务时
			if (tasks.size() == 0) {
				try {
					synchronized (this) {
						suspended = true;
						wait();
					}
				} catch (InterruptedException e) {
					 logger.error("", e);
				}
			} else {
				if (sleepTime >= 0) {
					try {
						sleeping = true;
						sleep(sleepTime);
						sleepTime = scan();
						sleeping = false;
					} catch (InterruptedException i) {
						 logger.error("", i);
					}
				}
			}
		}
	}

	/**
	 * 添加一个任务
	 */
	public void add(TimerRunnable client) {
		TimerTask t = new TimerTask(client);
		synchronized (tasks) {
			tasks.addElement(t);
		}

		if (suspended) {// 启用线程
			synchronized (this) {
				notify();
				suspended = false;
			}
		}
	}

	/**
	 * 暂停 指定 定时器
	 */
	public void suspended(TimerRunnable client) {
		synchronized (tasks) {
			for (int i = 0; i < tasks.size(); i++) {
				TimerTask t = (TimerTask) tasks.elementAt(i);
				if (t.isDeletePending() == false && t.getClient() == client) {
					t.suspended();
				}
			}
		}
	}

	/**
	 * 定时器 解除暂停状态
	 */
	public void start(TimerRunnable client) {
		synchronized (tasks) {
			for (int i = 0; i < tasks.size(); i++) {
				TimerTask t = (TimerTask) tasks.elementAt(i);
				if (t.isDeletePending() == false && t.getClient() == client) {
					t.start();
				}
			}
		}
	}

	/**
	 * 根据对象 和 任务id 找出 任务队列中的任务 将属性设为删除 在线程下次执行时 会自动将该任务删除
	 */
	public boolean end(TimerRunnable client) {
		synchronized (tasks) {
			for (int i = 0; i < tasks.size(); i++) {
				TimerTask t = (TimerTask) tasks.elementAt(i);
				if (t.isDeletePending() == false && t.getClient() == client) {
					t.setRepeat(false);
					t.setDeletePending(true);
					return true;
				}
			}
			return false;
		}
	}

	public void purge() {// 清除所有需要删除的任务
		for (int i = 0; i < tasks.size(); i++) {
			TimerTask t = (TimerTask) tasks.elementAt(i);
			if (t.isDeletePending()) {
				tasks.removeElementAt(i);
				i--;
			}
		}
	}

	public long scan() {// 当前任务中距离执行时间最短的任务时间 需要的空闲时间 如果空闲时间 较长 则睡 100毫秒
		long nextTime = System.currentTimeMillis() + 100;
		for (int i = 0; i < tasks.size(); i++) {
			TimerTask t = (TimerTask) tasks.elementAt(i);
			if (!t.isDeletePending() && !t.isSuspended()) {
				t.test();
				nextTime = Math.min(nextTime, t.getTimeNext());
			}
		}

		return nextTime - System.currentTimeMillis();
	}

	public Vector<TimerTask> getTasks() {
		return tasks;
	}

	public boolean isSuspended() {
		return suspended;
	}

	public boolean isSleeping() {
		return sleeping;
	}
}